﻿import time, sys

from platform.jutils import *
import bgtp.bgtp as bgtp

"""
вариант текстового клиента - ввод в консоль посылает серверу, добавляя жестко зашитый логин пользователя
на прием подписывает вывод в консоль
выход - по команде exit
"""

def getGUID():
	return "User2"

class Client:
	def __init__(this):
		this.guid = getGUID()
	
	def start(this):
		liba.init()
		liba.subscribe({}, 'stdout')
		liba.startClient()
	
	def stop(this):
		liba.stop()
		sys.exit()
	
	def send(this, msg, data = ""):
		inf = msg + ";" + data
		try:
			liba.send(inf)
		except:
			print "can't send to server"
	
	def receve(this, usr, msg, data):
		print "msg:", msg, data

if __name__ == '__main__':
	if sys.platform.count("java") == 0:
		sys.setcheckinterval(1) 
	liba = bgtp.liba
	c = Client()
	c.start()
	time.sleep(0.3)
	stop = False
	

	c.send("1;hi")							;time.sleep(0.5)
	c.send("2;hi")							;time.sleep(0.5)
	

	c.send('1;newGame;"7UP"')				;time.sleep(0.5)
	c.send("1;addGamer")					;time.sleep(0.5)
	c.send("2;addGamer")					;time.sleep(0.5)
		
	c.send('1;setGamerParty;"X"')			;time.sleep(0.5)
	c.send('2;setGamerParty;"O"')			;time.sleep(0.5)
	c.send("1;startGame")					;time.sleep(0.5)
	
	while not stop:
		command = sys.stdin.readline().strip()
		if command == "close":
			stop = True
			c.stop()
		else:
			c.send(command)